package com.buer.storage.controller;

import com.buer.storage.commom.JsonResult;
import com.buer.storage.dto.*;
import com.buer.storage.entity.Dorder;
import com.buer.storage.entity.Stock;
import com.buer.storage.entity.Worder;
import com.buer.storage.service.DestroyService;
import com.buer.storage.vo.R;
import com.github.pagehelper.PageInfo;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/destroy")
public class DestroyController {
    @Resource
    private DestroyService destroyService;

    @GetMapping("/findStock.do")
    public List<Stock> findStock(){
        return destroyService.findStock();
    }

    //根据出库ID查询
    @PostMapping("/selectByAll.do")
    public JsonResult selectByAll(@RequestBody Map<String,Object> params){
        PageInfo<Dorder> entityList = destroyService.selectByAll(params);
        Map result=new HashMap<>();
        result.put("total",entityList.getTotal());
        result.put("list",entityList.getList());
        return new JsonResult(1,result);
    }

    @PostMapping("/insert.do")
    public R insert(@RequestBody DeStorageDto deStorageDto){
        if (destroyService.insert(deStorageDto) > 0) {

            return R.setR(1, "报废单保存成功", null);
        }

        return R.setR(0,"报废单保存失败",null);
    }

    @PostMapping("/findDestroyAll.do")
    public JsonResult findDestroyAll(@RequestBody Map<String,Object> params){
        PageInfo<DestroyDto> entityList = destroyService.findDestroyAll(params);
        Map result=new HashMap<>();
        result.put("total",entityList.getTotal());
        result.put("list",entityList.getList());
        return new JsonResult(1,result);
    }

    //根据订单ID查询入库信息
    @GetMapping("/findOutAll.do")
    public List<DeStorageAllDto> findOutAll(Integer destroyId){
        List<DeStorageAllDto> outAll = destroyService.findOutAll(destroyId);
        return outAll;
    }

    //根据入库订单ID删除出库信息
    @GetMapping("/deleteDestroyById.do")
    public R deleteDestroyById(int destroyId) {
        if (destroyService.deleteDestroyById(destroyId) > 0) {
            return R.setR(1, "删除报废单成功", null);
        }
        return R.setR(0, "删除报废单失败", null);
    }

    //根据商品ID删除商品信息
    @GetMapping("/deleteDestroydetails.do")
    public R deleteDestroydetails(int detailsId) {
        if (destroyService.deleteDestroydetails(detailsId) > 0) {
            return R.setR(1, "删除报废订单成功", null);
        }
        return R.setR(0, "删除报废订单失败", null);
    }

    //更新ID删除商品信息
    @PostMapping("/updateDestroydetails.do")
    public R updateDestroydetails(@RequestBody DeStorageDto deStorageDto) {
        if (destroyService.updateDestroydetails(deStorageDto) > 0) {
            return R.setR(1, "更新报废单成功", null);
        }
        return R.setR(0, "更新报废单失败", null);
    }

    //提交商品详情进行累加
    @GetMapping("/submitAddDestroydetails.do")
    public R submitAddBuerStock(int destroyId) {
        if (destroyService.submitAddBuerStock(destroyId) > 0) {
            return R.setR(1, "报废成功", null);
        }
        return R.setR(0, "报废失败", null);
    }
}
